<template>
  <div class="offset-curve-container">
    <div class="scroll-wrapper">
      <div class="curve-container" ref="curveContainerRef">
        <svg ref="svgRef" class="offset-curve-svg" :width="svgWidth" :height="height">
          <!-- 坐标轴 -->
          <g class="coordinate-system">
            <!-- Y轴 -->
            <line x1="40" y1="20" x2="40" :y2="height - 30" class="axis" />
            <!-- X轴 -->
            <line x1="40" :y1="height - 30" :x2="svgWidth - 20" :y2="height - 30" class="axis" />
            
            <!-- Y轴刻度和标签 -->
            <g class="y-axis-labels">
              <template v-for="i in 7" :key="`y-label-${i}`">
                <line
                  x1="35"
                  :y1="20 + (i - 1) * ((height - 50) / 6)"
                  x2="40"
                  :y2="20 + (i - 1) * ((height - 50) / 6)"
                  class="tick"
                />
                <text
                  x="30"
                  :y="20 + (i - 1) * ((height - 50) / 6)"
                  class="axis-label"
                  text-anchor="end"
                  dominant-baseline="middle"
                >
                  {{ 300 - (i - 1) * 100 }}
                </text>
              </template>
              <!-- Y轴标题 -->
              <text
                x="10"
                :y="height / 2"
                class="axis-title"
                text-anchor="middle"
                transform="rotate(-90, 10, height / 2)"
              >
                
              </text>
            </g>
          </g>

          <!-- 网格线 -->
          <g class="grid-lines">
            <template v-for="i in 20" :key="`x-${i}`">
              <line
                :x1="getXPosition(i)"
                y1="20"
                :x2="getXPosition(i)"
                :y2="height - 30"
                class="grid-line"
              />
            </template>
            <template v-for="i in 6" :key="`y-${i}`">
              <line
                x1="40"
                :y1="20 + i * ((height - 50) / 6)"
                :x2="svgWidth - 20"
                :y2="20 + i * ((height - 50) / 6)"
                class="grid-line"
              />
            </template>
          </g>

          <!-- 中心线 -->
          <line
            x1="40"
            :y1="height / 2"
            :x2="svgWidth - 20"
            :y2="height / 2"
            class="center-line"
          />

          <!-- 桩桶标记和编号 -->
          <g class="cone-markers">
            <template v-for="i in 20" :key="`cone-${i}`">
              <circle
                :cx="getXPosition(i)"
                :cy="height / 2"
                r="4"
                class="cone-marker"
              />
              <text
                :x="getXPosition(i)"
                :y="height - 10"
                class="cone-number"
                text-anchor="middle"
              >
                {{ i }}
              </text>
            </template>
          </g>

          <!-- 偏移曲线 -->
          <path
            :d="curvePath"
            class="offset-curve"
            fill="none"
          />

          <!-- 偏移点 -->
          <g class="offset-points">
            <circle
              v-for="point in offsetPoints"
              :key="point.id"
              :cx="point.x"
              :cy="point.y"
              r="3"
              class="offset-point"
              @mouseover="showTooltip(point, $event)"
              @mouseleave="hideTooltip"
            />
          </g>
        </svg>
      </div>
    </div>

    <!-- 悬浮提示 -->
    <div
      v-if="tooltipVisible"
      class="tooltip"
      :style="tooltipStyle"
    >
      {{ tooltipContent }}
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'

const props = defineProps({
  width: {
    type: Number,
    default: 1000
  },
  height: {
    type: Number,
    default: 200
  },
  offsetData: {
    type: Object,
    default: () => ({})
  }
})

// SVG引用
const svgRef = ref(null)
const curveContainerRef = ref(null)

// 计算SVG宽度，确保每个桩桶有足够空间
const minConeWidth = 80 // 每个桩桶的最小宽度
const svgWidth = computed(() => Math.max(props.width, minConeWidth * 20 + 60))

// 获取X坐标位置
function getXPosition(index) {
  const totalWidth = svgWidth.value - 60 // 减去左右边距
  const xScale = totalWidth / 20
  return (index - 0.5) * xScale + 40
}

// 偏移点数据处理
const offsetPoints = ref([])
const curvePath = ref('')

// 提示框相关
const tooltipVisible = ref(false)
const tooltipContent = ref('')
const tooltipStyle = ref({
  left: '0px',
  top: '0px'
})

// 显示提示框
function showTooltip(point, event) {
  tooltipContent.value = `偏移: ${point.offsetValue}mm\n方向: ${point.direction === 'l' ? '左绕' : point.direction === 'r' ? '右绕' : '直行'}`
  tooltipStyle.value = {
    left: `${event.clientX + 10}px`,
    top: `${event.clientY - 10}px`
  }
  tooltipVisible.value = true
}

// 隐藏提示框
function hideTooltip() {
  tooltipVisible.value = false
}

// 计算偏移点和曲线
function calculateOffsetPoints() {
  const points = []
  const scale = (props.height - 50) / 600 // 300mm上下偏移，总高度600mm

  for (let i = 1; i <= 20; i++) {
    const idStr = i.toString().padStart(3, '0')
    const direction = props.offsetData[`${idStr}dir`] || 'n'
    const value = parseInt(props.offsetData[`${idStr}val`] || '0', 10)

    const x = getXPosition(i)
    let y = props.height / 2

    if (direction === 'l') {
      y -= value * scale
    } else if (direction === 'r') {
      y += value * scale
    }

    points.push({
      id: i,
      x,
      y,
      direction,
      offsetValue: value
    })
  }

  offsetPoints.value = points
  calculateCurvePath(points)
}

// 计算曲线路径
function calculateCurvePath(points) {
  if (points.length < 2) return ''

  let path = `M ${points[0].x} ${points[0].y}`
  
  for (let i = 0; i < points.length - 1; i++) {
    const current = points[i]
    const next = points[i + 1]
    const cp1x = current.x + (next.x - current.x) / 3
    const cp2x = current.x + 2 * (next.x - current.x) / 3
    
    path += ` C ${cp1x},${current.y} ${cp2x},${next.y} ${next.x},${next.y}`
  }

  curvePath.value = path
}

// 监听偏移数据变化
watch(() => props.offsetData, () => {
  calculateOffsetPoints()
}, { deep: true })

// 组件挂载时初始化
onMounted(() => {
  calculateOffsetPoints()
})
</script>

<style scoped>
.offset-curve-container {
  position: relative;
  width: 100%;
  background: white;
  border-radius: 8px;
  padding: 20px 30px 40px 50px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.scroll-wrapper {
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
}

.curve-container {
  min-width: 100%;
  position: relative;
}

.offset-curve-svg {
  display: block;
  min-width: 100%;
}

.grid-line {
  stroke: #f0f0f0;
  stroke-width: 1;
}

.center-line {
  stroke: #e0e0e0;
  stroke-width: 2;
  stroke-dasharray: 5,5;
}

.cone-marker {
  fill: #909399;
  stroke: white;
  stroke-width: 1;
}

.offset-curve {
  stroke: #409EFF;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 2px 4px rgba(64, 158, 255, 0.2));
}

.offset-point {
  fill: #409EFF;
  stroke: white;
  stroke-width: 1;
  cursor: pointer;
  transition: r 0.2s;
}

.offset-point:hover {
  r: 5;
}

.axis {
  stroke: #303133;
  stroke-width: 2;
}

.tick {
  stroke: #303133;
  stroke-width: 1;
}

.axis-label {
  font-size: 12px;
  fill: #606266;
}

.axis-title {
  font-size: 14px;
  fill: #303133;
  font-weight: 500;
}

.cone-number {
  font-size: 12px;
  fill: #606266;
  font-weight: 500;
}

.tooltip {
  position: fixed;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 12px;
  pointer-events: none;
  z-index: 1000;
  white-space: pre-line;
}

/* 自定义滚动条样式 */
.scroll-wrapper::-webkit-scrollbar {
  height: 8px;
}

.scroll-wrapper::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.scroll-wrapper::-webkit-scrollbar-thumb {
  background: #409EFF;
  border-radius: 4px;
}

.scroll-wrapper::-webkit-scrollbar-thumb:hover {
  background: #337ecc;
}
</style> 